package com.example.rotateimageview.marquee

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet

import androidx.appcompat.widget.AppCompatTextView
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent

/**
 *
 */
class NewMarqueeTextView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : AppCompatTextView(context, attributeSet) {

    private val mPaint = Paint()

    private var viewWidth = 0f

    private var currentTextWidth = 0f

    private var startFlag = false

    private var currentTextContent = ""

    private var tempScrollX1 = 0f //控制本来的文字的绘制位置

    private var tempScrollX2 = 0f //控制滚出左面时 右面出来的文字

    private var textY = 0f

    private var needDrawTempScroll2 = false // true的时候 头尾都画文字

    private var isFullViewText = false //文字长度是否显示的下

    private var intervalDistance = 40f

    private var totalTextAndViewWidth = 0f

    private var scrollSpeed = 1.5f //滚动速度

    init {
        mPaint.apply {
            isAntiAlias = true
            style = Paint.Style.FILL
            textAlign = Paint.Align.CENTER
        }
    }


    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas!!.drawText(currentTextContent, viewWidth - tempScrollX1, textY, paint)
        if (needDrawTempScroll2) {
            canvas.drawText(currentTextContent, viewWidth - tempScrollX2, textY, paint)
        }

        if (!startFlag) {
            return
        }

        tempScrollX1 += scrollSpeed

        if (tempScrollX1 > totalTextAndViewWidth) {
            //左边完全滚完
            tempScrollX1 = tempScrollX2
            needDrawTempScroll2 = false
        }

        if (needDrawTempScroll2) {
            tempScrollX2 += scrollSpeed
        } else {
            if (isFullViewText) {
                if (tempScrollX1 > currentTextWidth + intervalDistance) {
                    needDrawTempScroll2 = true
                    tempScrollX2 = 0f
                }
            } else {
                if (tempScrollX1 > viewWidth) {
                    needDrawTempScroll2 = true
                    tempScrollX2 = 0f
                }
            }
        }
        invalidate()
    }


    fun setCurrentTextContent(content: String) {
        this.currentTextContent = content
    }


    fun startScroll(lifecycle: Lifecycle? = null) {
        startFlag = true
        if (currentTextContent.isNotEmpty()) {
            measureLength()
            invalidate()
        }

        lifecycle?.addObserver(object :LifecycleObserver{
            @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
            fun onPause() {
                stopScroll()
            }

            @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
            fun onDestroy() {
                lifecycle?.removeObserver(this)
            }
        })
    }

    fun stopScroll() {
        startFlag = false
    }

    private fun measureLength() {
        viewWidth = measuredWidth.toFloat()
        currentTextWidth = paint.measureText(currentTextContent)
        tempScrollX1 = currentTextWidth
        totalTextAndViewWidth = currentTextWidth + viewWidth
        isFullViewText = if (currentTextWidth > viewWidth) {
            true
        } else {
            currentTextWidth + intervalDistance > viewWidth
        }
        textY = textSize + paddingTop
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        measureLength()
    }
}